import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class JFrameDemo3 {
    public static void main(String[] args) {
        JFrame jf = new JFrame("聊天室");
        jf.setSize(400,300);
        jf.setLocationRelativeTo(null);
        jf.setAlwaysOnTop(true);
        jf.setLayout(null);
        jf.setDefaultCloseOperation(3);

        JTextArea jTextArea = new JTextArea();
        jTextArea.setBounds(10,10,360,200);
        jf.add(jTextArea);

        JTextField jTextField = new JTextField();
        jTextField.setBounds(10,230,180,20);
        jf.add(jTextField);

        JButton send = new JButton("发送");
        send.setBounds(200,230,70,20);
        jf.add(send);

        JButton clean = new JButton("清空聊天");
        clean.setBounds(280,230,100,20);
        jf.add(clean);

        send.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String a = jTextField.getText();
                if(a.isEmpty()){
                    return;
                }
                jTextField.setText("");
                jTextArea.append(a + "\n");
            }
        });

        clean.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                jTextArea.setText("");
            }
        });

        jf.setVisible(true);
    }
}
